package com.rzjm.api.service.impl;

import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.rzjm.api.dao.DevicePowerConsumptionDao;
import com.rzjm.api.entity.Device;
import com.rzjm.api.entity.DevicePowerConsumption;
import com.rzjm.api.service.DevicePowerConsumptionService;
import com.rzjm.api.util.DateUtil;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.*;

@Service
public class DevicePowerConsumptionServiceImpl extends BaseServiceImpl<DevicePowerConsumptionDao, DevicePowerConsumption>
        implements DevicePowerConsumptionService {

    @Resource
    private DevicePowerConsumptionDao powerConsumptionDao;

    @Override
    public Map<String, Object> getChartDataByDevice(Device device, String day, String beginHour, String endHour) {
        double maxValue = 0d;
        Map<String,Object> mapReturn = new HashMap<>();
        List<String> times = new ArrayList<>();
        List<String> voltages = new ArrayList<>();
        List<String> electrics = new ArrayList<>();
        List<String> powers = new ArrayList<>();

        QueryWrapper<DevicePowerConsumption> qw = new QueryWrapper<>();
        qw.eq("device_id", device.getId());
        qw.between("create_time",day + " "+ beginHour +":00",day + " "+  endHour +":59");
        qw.orderByAsc("create_time");
        List<DevicePowerConsumption> powerConsumptions = powerConsumptionDao.selectList(qw);
        if(CollUtil.isNotEmpty(powers)){
            for (DevicePowerConsumption power : powerConsumptions){
                times.add(DateUtil.getSimpleTime1(power.getCreateTime()));
                voltages.add(power.getVoltage().toString());
                electrics.add(power.getElectric().toString());
                powers.add(power.getPower().toString());
            }
        }else{
            //如果在这个区间没有数据，查询最后的时间段，保证有数据
            QueryWrapper<DevicePowerConsumption> qwReset = new QueryWrapper<>();
            qwReset.eq("device_id", device.getId());
            qwReset.orderByDesc("create_time");
            qwReset.last("limit 1");
            DevicePowerConsumption powerConsumption = powerConsumptionDao.selectOne(qwReset);
            if(powerConsumption != null){
                Date lastRecord = powerConsumption.getCreateTime();
                day = DateUtil.getDayStr(lastRecord);

                Calendar calendar = Calendar.getInstance();
                calendar.setTime(lastRecord);
                calendar.add(Calendar.HOUR_OF_DAY,-1);
                beginHour = calendar.get(Calendar.HOUR_OF_DAY) + ":00";

                Calendar calendarEnd = Calendar.getInstance();
                calendarEnd.setTime(lastRecord);
//                calendarEnd.add(Calendar.HOUR_OF_DAY,1);
                endHour = calendarEnd.get(Calendar.HOUR_OF_DAY) + ":59";

                QueryWrapper<DevicePowerConsumption> qwNew = new QueryWrapper<>();
                qwNew.eq("device_id", device.getId());
                qwNew.between("create_time",day + " "+ beginHour +":00",day + " "+  endHour +":59");
                qwNew.orderByAsc("create_time");
                List<DevicePowerConsumption> powerNews = powerConsumptionDao.selectList(qwNew);
                if(CollUtil.isNotEmpty(powerNews)){
                    for (DevicePowerConsumption powerConsumption1 : powerNews){
                        times.add(DateUtil.getSimpleTime1(powerConsumption1.getCreateTime()));
                        voltages.add(powerConsumption1.getVoltage().toString());
                        electrics.add(powerConsumption1.getElectric().toString());
                    }
                }
            }
        }

//        QueryWrapper<DeviceNode> qwNode = new QueryWrapper<>();
//        qwNode.eq("device_id",deviceId);
//        qwNode.eq("node_index",node.getNodeIndex());
//        qwNode.eq("ins_index",node.getInsIndex());
//        qwNode.last("limit 1");
//        DeviceNode deviceNode = deviceNodeDao.selectOne(qwNode);
//        if(deviceNode!=null){
//            mapReturn.put("node_name",deviceNode.getTerminalName());
//            //设备类型  2 - 测温度 3 - 测电流  4 - 测重量  5 - 雷达测距
//            if(deviceNode.getDeviceType() == 2){
//                mapReturn.put("data_title", "温度");
//                mapReturn.put("data_unit", "℃");
//            } else if(deviceNode.getDeviceType() == 3){
//                mapReturn.put("data_title", "电流");
//                mapReturn.put("data_unit", "A");
//            }
//        }

        mapReturn.put("canvasImg","");
        mapReturn.put("times",times);
        mapReturn.put("voltages",voltages);
        mapReturn.put("electrics",electrics);
        mapReturn.put("powers",powers);
        mapReturn.put("count",times.size());
        mapReturn.put("maxValue",maxValue);
        mapReturn.put("minValue",maxValue);

        mapReturn.put("day",day);
        mapReturn.put("beginHour",beginHour);
        mapReturn.put("endHour",endHour);

        return mapReturn;
    }
}
